/** @file    FormBundle.cpp
 *  @author  Alessandro Polo
 *  @version $Id: FormBundle.cpp 2748 2010-07-11 22:07:06Z alex $
 *  @brief
 * File containing methods for the wosh::gui::FormBundle class.
 * The header for this class can be found in FormBundle.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

 #include "FormBundle.h"
 #include "ui_FormBundle.h"

 #include <core/ObjectAllocator.h>
 #include <core/MethodsCommon.h>
 #include <core/Request.h>
 #include <core/Response.h>
 #include <core/Table.h>
 #include <core/LogContext.h>

 #include <QtGui>


using namespace std;
namespace wosh {
 namespace gui {

// WOSH_GUI_MAP(wosh::gui::FormBundle, "wosh::Bundle", 0.0, _static_Map_FormBundle )
 WOSH_REGISTER(wosh::gui::FormBundle, "wosh::gui::IWidgetGeneric", 1.0, _static_FormBundle )
 WOSH_GUI_MAP(wosh::gui::FormBundle, "wosh::BundleGeneric", 0.0, _static_Map_FormBundle )

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

FormBundle::FormBundle(QWidget *parent) : IWidgetGeneric(parent)
 {
	this->m_ui = new Ui::FormBundle();
	this->m_ui->setupUi(this);

	// tab 0
	connect(this->m_ui->wUpBarInformation, SIGNAL(updateClicked()), this, SLOT(refreshInformation()));
	mapMethodButton(_Bundle_METHOD_Start, this->m_ui->btnCtrlStart);
	mapMethodButton(_Bundle_METHOD_Restart, this->m_ui->btnCtrlReStart);
	mapMethodButton(_Bundle_METHOD_Stop, this->m_ui->btnCtrlStop);
	mapMethodButton(_Bundle_METHOD_Diagnostic, this->m_ui->btnCtrlDiagnostic);
	connect(this->m_ui->cmbDebugLevel, SIGNAL(activated(const QString &)), this, SLOT(cmbDebugLevel_activated(const QString &)));
	this->propMapper = new PropertiesMapper(this);
	this->propMapper->getMap().insert(_KEY_name, this->m_ui->lblName);
	this->propMapper->getMap().insert(_KEY_Version, this->m_ui->lblVersion);
	this->propMapper->getMap().insert(_KEY_type, this->m_ui->lblType);
	this->propMapper->getMap().insert(_Bundle_KEY_State, this->m_ui->lblState);
	this->propMapper->getMap().insert(_KEY_DebugLevel, this->m_ui->cmbDebugLevel);

	// tab Properties [1]
	// tab Methods [2]

	// tab Interfaces&Requirements [3]
	this->m_ui->tableViewInterfaces->setMethodList(_METHOD_ListInterfaces);
	connect(this->m_ui->tableViewInterfaces, SIGNAL(postMessage(Message*)),this, SLOT(postMessage(Message*)) );
	this->m_ui->tableViewRequirements->setMethodList(_METHOD_ListRequirements);
	connect(this->m_ui->tableViewRequirements, SIGNAL(postMessage(Message*)),this, SLOT(postMessage(Message*)) );

 }

FormBundle::~FormBundle() {
	delete this->propMapper;
	delete this->m_ui;
 }

void FormBundle::changeEvent(QEvent *e) {
	QWidget::changeEvent(e);
	if ( e->type() == QEvent::LanguageChange )
		this->m_ui->retranslateUi(this);
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormBundle::cmbDebugLevel_activated(const QString& text) {
	List* dlProp = new List(2);
	dlProp->push_back<Variant>( _KEY_DebugLevel );
	dlProp->push_back<Variant>( string(text.toLatin1().data()) );
	Request* request_info = new Request( _METHOD_SetProperty, dlProp );
	postMessageForTarget( new Message(request_info) );
	this->m_ui->wUpBarInformation->showMessage("Changing DebugLevel..");
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormBundle::setTargetURI( const URI& target_uri ) {
	IWidgetGeneric::setTargetURI(target_uri);
	this->m_ui->wwCore_PropertiesProvider->setTargetURI(target_uri);
	this->m_ui->wWidgetMethods->setTargetURI(target_uri);
	this->m_ui->lblURI->setText( QString(target_uri.toString().c_str()) );
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormBundle::refreshAll() {
	refreshInterfaces();
	refreshRequirements();
	this->m_ui->wwCore_PropertiesProvider->refreshAll();
	this->m_ui->wWidgetMethods->refreshAll();
 }

WRESULT FormBundle::init() {
	WRESULT ret = IWidget::init();
	ret += this->m_ui->wwCore_PropertiesProvider->init();
	ret += this->m_ui->wWidgetMethods->init();
	return ret;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormBundle::refreshInformation() {
	List* dlProps = this->propMapper->createKeysList();
	executeRemoteMethod(_METHOD_GetProperties, dlProps);
	this->m_ui->wUpBarInformation->sayRetrieving();
 }
void FormBundle::refreshInterfaces() {
	this->m_ui->tableViewInterfaces->refreshData();
 }
void FormBundle::refreshRequirements() {
	this->m_ui->tableViewRequirements->refreshData();
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FormBundle::busMessage( const Message& message, const Bus* ) {
	if ( !message.getContent()->isResponse() ) return;
	if ( !isMessageForMeOrFromTarget(message) )
		return;
	const wosh::Response* response = message.getContent()->asResponse();

	if ( promptMethodResponse(message) )
		refreshInformation();

	if ( this->propMapper->evalResponse(response) )
		this->m_ui->wUpBarInformation->sayReady();

	this->m_ui->tableViewInterfaces->evalResponse(response);
	this->m_ui->tableViewRequirements->evalResponse(response);

 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 }; // namespace gui
}; // namespace wosh
